package xiaodou.core.WeiboData
{
	import com.sina.microblog.data.MicroBlogStatus;
	
	import flash.utils.Dictionary;
	
	import spark.formatters.DateTimeFormatter;
	
	import xiaodou.core.WeiboData.events.WeiboDataEvent;
	import xiaodou.core.WeiboData.events.WeiboEvent;
	

	
	public class WeiboStatusData
	{
		/**
		 *保存微博内容的数组 
		 */		
		protected var AryStatus:Array = new Array();
		
		protected var _TimeCursorFront:Object;
		
		protected var _TimeCursorBack:Object;
		
		protected var DateFormat:DateTimeFormatter = new DateTimeFormatter();
		
		public function WeiboStatusData()
		{
			_TimeCursorFront = { date: new Date(2000,1,1), cursorStatusID:-1};
			_TimeCursorBack = { date: new Date(), cursorStatusID:-1};
			DateFormat.dateTimePattern = "yyyy-MM-dd";
		}
		
		public function RemoveAllData():void
		{
			AryStatus.splice(0);
			
			_TimeCursorFront = { date: new Date(2000,1,1), cursorStatusID:-1};
			_TimeCursorBack = { date: new Date(), cursorStatusID:-1};
		}
		
		public function GetStatusByDate(from:Date, to:Date):Array
		{
			if(from < _TimeCursorBack.date)
				from = _TimeCursorBack.date;
			
			if(to > _TimeCursorFront.Date)
				to = _TimeCursorFront.Date;
			
			
			var ary:Array = new Array();
			for(var i:int = 0; i < AryStatus.length; ++ i)
			{
				var commentCreatedAt:Date = new Date(Date.parse(AryStatus[i]["createdAt"]));
				if(commentCreatedAt >= from  && commentCreatedAt <= to)
				{
					ary.push(AryStatus[i]);
				}
			}
			
			return ary;
		}
		
		/**
		 *返回微博的总数 
		 * @return 
		 * 
		 */		
		
		public function get TotalNum():int
		{
			return AryStatus.length;
		}
		
		/**
		 * 记录所存微博中时间游标，发布最早的微博的时间
		 */
		public function get TimeCursorBack():Date
		{
			return _TimeCursorBack.date;
		}
		
		/**
		 *记录所存微博中时间游标，发布最晚的微博的时间 
		 */
		public function get TimeCursorFront():Date
		{
			return _TimeCursorFront.date;
		}
		
		/**
		 * 记录所存微博中时间游标，发布最早的微博的ID
		 */
		public function get TimeCursorBackID():String
		{
			return _TimeCursorBack.cursorStatusID;
		}
		
		/**
		 *记录所存微博中时间游标，发布最晚的微博的ID 
		 */
		public function get TimeCursorFrontID():String
		{
			return _TimeCursorFront.cursorStatusID;
		}
		
		/**
		 *添加一条信息到记录中 
		 * @param status
		 * 
		 */		
		public function PushNewStatus(status:Object):void
		{
			var statusCreatedAt:Date = new Date(Date.parse(status["created_at"]));
			var newStatus:MicroBlogStatus = new MicroBlogStatus(status);
			
			var Length:int = AryStatus.push(newStatus);
			var Index:int = Length - 1;
			if(statusCreatedAt < TimeCursorBack)
			{
				_TimeCursorBack = {
					date		  :statusCreatedAt,
					cursorStatusID:status["id"]
				}
			}
			
			if(statusCreatedAt > TimeCursorFront)
			{
				_TimeCursorFront = {
					date		  :statusCreatedAt,
					cursorStatusID:status["id"]
				}
			}
			
			
		}
		
	}
}